#include "mine_clear.h"
#include "QWidget"
#include <QApplication>
#include "QPixmap"
#include "QPainter"
#include "QMap"
#include "qtimer.h"
#include "QtGlobal"
#include "QTimer"
#include "QMouseEvent"
#include "QRandomGenerator"

const int ROW_COUNT=9;
const int COL_COUNT=9;
const int BLOCK_WIDTH=32;
const int BLOCK_HEIGHT=32;
const int MINE_COUNT=10;
const int WINDOW_WIDTH=ROW_COUNT*BLOCK_WIDTH;
const int WINDOW_HEIGHT=COL_COUNT*BLOCK_HEIGHT;

enum block{
    zero,one,two,three,four,five,six,seven,eight,mine,flag,unshown
};



QMap<block,QPixmap> map;


inline bool isnum(block t)
{
    return t==one||t==two||t==three||t==four||t==five||t==six||t==seven||t==eight;
}
class Window: public QWidget{
public:

    block data[ROW_COUNT][COL_COUNT];
    block showmap[ROW_COUNT][COL_COUNT];
    bool gameover=false;
    Window(){
        setWindowTitle("扫雷");
        setFixedSize(WINDOW_WIDTH,WINDOW_HEIGHT);

        //游戏地图全部置零，初始化
        for(int i=0;i<ROW_COUNT;i++){
            for(int j=0;j<COL_COUNT;j++){
                    data[i][j]=zero;
                    showmap[i][j]=unshown;
            }
         }

        //随机生成10个炸弹
        for(int i=0;i<MINE_COUNT;i++){
          int pos=QRandomGenerator::global()->generate()%81;
          int x=pos/ROW_COUNT;
          int y=pos%ROW_COUNT;
            data[x][y]=mine;
         }


        //遍历数组，将每一格的周围雷数统计出来
        for(int i=0;i<ROW_COUNT;i++){
            for(int j=0;j<COL_COUNT;j++){
                  int count=0;

                  if(data[i][j]==mine)
                  {
                      continue;
                  }


                  for(int m=i-1;m<=i+1;m++)
                  {
                      for(int n=j-1;n<=j+1;n++)
                      {
                          //这里判断是否越界
                          if(m>=0 && m<ROW_COUNT && n>=0 && n<COL_COUNT && data[m][n]==mine)
                          {
                              count++;
                          }
                      }
                  }
                  switch(count)
                  {
                  case 0:data[i][j]=zero;
                      break;
                  case 1:data[i][j]=one;
                      break;
                  case 2:data[i][j]=two;
                      break;
                  case 3:data[i][j]=three;
                      break;
                  case 4:data[i][j]=four;
                      break;
                  case 5:data[i][j]=five;
                      break;
                  case 6:data[i][j]=six;
                      break;
                  case 7:data[i][j]=seven;
                      break;
                  case 8:data[i][j]=eight;
                      break;
                  }

            }
         }
    };



    void paintEvent(QPaintEvent *event) override{

        Q_UNUSED(event);
        QPainter painter(this);
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                painter.drawPixmap(i*BLOCK_WIDTH+qMin(1,i),j*BLOCK_HEIGHT+qMin(1,j),
                                   gameover ?map[data[i][j]]:map[showmap[i][j]]);
            }
        }

    }

    //翻开格子消除
    void clear(int x,int y){
        if(gameover){return;}
        if(showmap[x][y]!=unshown){
            return;
        }
        showmap[x][y]=data[x][y];
        if(data[x][y]==mine)
        {
            gameover=true;
            update();
            return;
        }
        showmap[x][y]=data[x][y];
        //检索周围格子的情况
        for(int m=x-1;m<=x+1;m++)
        {
            for(int n=y-1;n<=y+1;n++)
            {
                //搜索到本格跳过
                if(m==x&&n==y)
                {
                    continue;
                }
                if(m>=0 && m<ROW_COUNT && n>=0 && n<COL_COUNT)
                {


                    //递归搜索，适合周边数字为0的情况
                    if(data[m][n]==zero)
                    {
                        clear(m,n);
                    }
                    //搜索到数字停止
                    else if(isnum(data[m][n]))
                    {
                        showmap[m][n]=data[m][n];
                        continue;
                    }
                }
            }
        }
    }

//获取鼠标动作
    void mousePressEvent(QMouseEvent *event) override{

        if(gameover){
            return;
        }
        int x=event->x()/BLOCK_WIDTH;
        int y=event->y()/BLOCK_HEIGHT;
        if(event->button()==Qt::LeftButton && showmap[x][y]==unshown){

            clear(x,y);
            update();
        }
        else if(event->button()==Qt::RightButton){
            showmap[x][y]=showmap[x][y]==flag?unshown:flag;
            update();
        }
    }
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //加载图片
    auto *infopicture= new QPixmap("://new/image/infopicture.jpg");
    map.insert(zero,infopicture->copy(0,0,32,32));
    map.insert(one,infopicture->copy(32*1,0,32,32));
    map.insert(two,infopicture->copy(32*2,0,32,32));
    map.insert(three,infopicture->copy(32*3,0,32,32));
    map.insert(four,infopicture->copy(32*4,0,32,32));
    map.insert(five,infopicture->copy(32*5,0,32,32));
    map.insert(six,infopicture->copy(32*6,0,32,32));
    map.insert(seven,infopicture->copy(32*7,0,32,32));
    map.insert(eight,infopicture->copy(32*8,0,32,32));
    map.insert(mine,infopicture->copy(32*9,0,32,32));
    map.insert(unshown,infopicture->copy(32*10,0,32,32));
    map.insert(flag,infopicture->copy(32*11,0,32,32));

    Window windows;
    windows.show();
    return a.exec();
}
